package com.example.mobifin_busticket.views.dialogs;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.mobifin_busticket.BusTicketManagaer;
import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.models.BusModel;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class SendEmailDialog extends Dialog implements
		android.view.View.OnClickListener {
	EditText etEmail;
	Button btn_send_email;
	Context mcontext;
	String strEmail, stremailsub, stremailbody, strtag, strsms;

	public SendEmailDialog(Context context, String strPassengerEmail,
			String stremailsub, String stremailbody, String strtag,
			String strsms) {
		super(context);
		// TODO Auto-generated constructor stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.send_email_layout);
		this.mcontext = context;
		this.strEmail = strPassengerEmail;
		this.stremailsub = stremailsub;
		this.stremailbody = stremailbody;
		this.strtag = strtag;
		this.strsms = strsms;
		etEmail = (EditText) findViewById(R.id.etEmail);
		btn_send_email = (Button) findViewById(R.id.btn_send_email);
		btn_send_email.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_send_email:
			OnclickSendEmail();
			break;

		default:
			break;
		}
	}

	private void OnclickSendEmail() {
		// TODO Auto-generated method stub
		if (etEmail.getText().toString().trim().length() == 0) {

			etEmail.setError(mcontext.getResources().getString(
					R.string.emailvalmasg));
		} else if (etEmail.getText().toString().trim().length() > 0
				&& !emailValidation(etEmail.getText().toString().trim())) {

			etEmail.setError(mcontext.getResources().getString(
					R.string.pleasevalidemail));
		} else {
			dismiss();
			Log.d("strtag", "" + strtag);
			BusTicketManagaer busTicketManager=new BusTicketManagaer();
			busTicketManager.sendEmailOrSMS(mcontext,
                    etEmail.getText().toString(), stremailsub, stremailbody,
                    strtag, strsms);
		}

	}

	protected boolean emailValidation(String emailstring) {
		Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher emailMatcher = emailPattern.matcher(emailstring);
		return emailMatcher.matches();
	}
}
